<template>
  <div>
    <div id="echarts_5" style="width: 100%; height: 100%" ref="echartsFive"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  name: "echartsFive",
  //   data() {
  //     echartsFive: "";
  //   },

  data() {
    return {
      sortList: [],
    };
  },
  mounted() {
    this.getDeviceOrderMoneySum();
  },
  methods: {
    echartsFive() {
      var that = this;
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(this.$refs.echartsFive);

      var xData = (function () {
        var data = [
          that.sortList[0].device_name.slice(0, 2) +
            that.sortList[0].device_name.slice(-2),
          that.sortList[1].device_name.slice(0, 2) +
            that.sortList[1].device_name.slice(-2),
          that.sortList[2].device_name.slice(0, 2) +
            that.sortList[2].device_name.slice(-1),
          that.sortList[3].device_name.slice(0, 2) +
            that.sortList[3].device_name.slice(-2),
          that.sortList[4].device_name.slice(0, 2) +
            that.sortList[4].device_name.slice(-1),
        ];

        return data;
      })();

      var data = [
        that.sortList[0].money_sum,
        that.sortList[1].money_sum,
        that.sortList[2].money_sum,
        that.sortList[3].money_sum,
        that.sortList[4].money_sum,
      ];

      var option = {
        // backgroundColor: "#141f56",

        tooltip: {
          show: "true",
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.4)", // 背景
          padding: [8, 10], //内边距
          // extraCssText: 'box-shadow: 0 0 3px rgba(255, 255, 255, 0.4);', //添加阴影
          formatter: function (params) {
            if (params.seriesName != "") {
              return params.name + " ：  " + params.value + " 辆";
            }
          },
        },
        grid: {
          borderWidth: 0,
          top: 20,
          bottom: 35,
          left: 40,
          right: 10,
          textStyle: {
            color: "#fff",
          },
        },
        xAxis: [
          {
            type: "category",

            axisTick: {
              show: false,
            },

            axisLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.2)",
              },
            },
            axisLabel: {
              inside: false,
              textStyle: {
                color: "#bac0c0",
                fontWeight: "normal",
                fontSize: "12",
              },
              // formatter:function(val){
              //     return val.split("").join("\n")
              // },
            },
            data: xData,
          },
          {
            type: "category",
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            data: xData,
          },
        ],
        yAxis: {
          min: 10,
          type: "value",
          axisTick: {
            show: false,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.2)",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            },
          },
          axisLabel: {
            textStyle: {
              color: "#bac0c0",
              fontWeight: "normal",
              fontSize: "12",
            },
            formatter: "{value}",
          },
        },
        series: [
          {
            type: "bar",
            itemStyle: {
              normal: {
                show: true,
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#00c0e9",
                  },
                  {
                    offset: 1,
                    color: "#3b73cf",
                  },
                ]),
                barBorderRadius: 50,
                borderWidth: 0,
              },
              emphasis: {
                shadowBlur: 15,
                shadowColor: "rgba(105,123, 214, 0.7)",
              },
            },
            zlevel: 2,
            barWidth: "20%",
            data: data,
          },
          {
            name: "",
            type: "bar",
            xAxisIndex: 1,
            zlevel: 1,
            itemStyle: {
              normal: {
                color: "transparent",
                borderWidth: 0,
                shadowBlur: {
                  shadowColor: "rgba(255,255,255,0.31)",
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowOffsetY: 2,
                },
              },
            },
            barWidth: "20%",
            data: [1000, 1000, 1000, 1000, 1000],
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
      window.addEventListener("resize", function () {
        myChart.resize();
      });
    },

    async getDeviceOrderMoneySum() {
      let result = await this.$API.reqSelectDeviceOrderMoneySum();
      result.sort((a, b) => {
        return b.money_sum - a.money_sum;
      });
      this.sortList = result;
      this.echartsFive();
    },
  },
};
</script>
